//
//  JQFXHProtosggView.swift
//  Lishui_JieDianQianHua
//
//  Created by 卢小艳 on 2025/7/18.
//

import Foundation
import UIKit


typealias ProtocolNextBlock = ()->Void

class JQFXHProtosggView: UIView {
    var jqfxhProtocolhhNextBlock : ProtocolNextBlock?
    var jqfxhProtocoliiList : [JQFXHSrwwProtocolModel] = []
    let jqfxhVc : UIViewController?
    
    init(frame:CGRect ,list:[JQFXHSrwwProtocolModel],jqfxhVc:UIViewController) {
        self.jqfxhVc = jqfxhVc
        super.init(frame: frame)
        jqfxhProtocoliiList = list
        
        self.addSubview(jqfxhBgkkView)
        jqfxhBgkkView.snp.makeConstraints { make in
            make.edges.equalTo(self)
        }
        
        self.addSubview(jqfxhContentllView)
        jqfxhContentllView.snp.makeConstraints { make in
            make.left.right.bottom.equalTo(self)
            make.top.equalTo(self.snp.centerY).offset(-80)
        }
        
        jqfxhContentllView.addSubview(jqfxhAgreenmmButton)
        jqfxhAgreenmmButton.snp.makeConstraints { make in
            make.left.equalTo(jqfxhContentllView).offset(30)
            make.centerX.equalTo(jqfxhContentllView)
            make.height.equalTo(42)
            make.bottom.equalTo(jqfxhContentllView).offset(-UIApplication.shared.jqfxhSafeAreaInsets.bottom)
        }
        
        jqfxhContentllView.addSubview(jqfxhTitlennLabel)
        jqfxhTitlennLabel.snp.makeConstraints { make in
            make.top.equalTo(jqfxhContentllView).offset(15)
            make.left.equalTo(jqfxhContentllView).offset(15)
            make.right.equalTo(jqfxhContentllView).offset(-50)
        }
        jqfxhContentllView.addSubview(jqfxhCloseooBtn)
        jqfxhCloseooBtn.snp.makeConstraints { make in
            make.top.equalTo(jqfxhContentllView).offset(15)
            make.right.equalTo(jqfxhContentllView).offset(-10)
        }
        
        jqfxhContentllView.addSubview(jqfxhTableppView)
        jqfxhTableppView.snp.makeConstraints { make in
            make.left.right.equalTo(jqfxhContentllView)
            make.top.equalTo(jqfxhTitlennLabel.snp.bottom).offset(15)
            make.bottom.equalTo(jqfxhAgreenmmButton.snp.top).offset(-15)
        }
        
    }
    
    @objc func jqfxhNextBtnClick() {
        self.jqfxhProtocolhhNextBlock?()
    }
    
    @objc func closeSelf() {
        self.removeFromSuperview()
    }
    
    
    lazy var jqfxhContentllView : UIView = {
        let jqfxhContentllView = UIView()
        jqfxhContentllView.backgroundColor = UIColor.white
        return jqfxhContentllView
    }()
    
    lazy var jqfxhCloseooBtn : UIButton = {
        let jqfxhCloseooBtn = UIButton(type: .custom)
        jqfxhCloseooBtn.setTitle("关闭", for: .normal)
        jqfxhCloseooBtn.setTitleColor(UIColor.black, for: .normal)
        jqfxhCloseooBtn.frame = CGRect(x: 20, y: 0, width: 40, height: 50)
        jqfxhCloseooBtn.addTarget(self, action: #selector(closeSelf), for: .touchUpInside)
        return jqfxhCloseooBtn
    }()
//
    lazy var jqfxhTitlennLabel : UILabel = {
        let navTitleLabel = UILabel(frame: CGRectMake(0, 0, jqfxhScreenWidth, 34))
        navTitleLabel.font = UIFont.jdqhBoldTitle16
        navTitleLabel.textColor = UIColor.black
        navTitleLabel.numberOfLines = 0
        navTitleLabel.text = "为了保护您的权益，请您先查看我们的协议内容，确认后点击同意以继续使用。"
        return navTitleLabel
    }()
    
    lazy var jqfxhBgkkView : UIView = {
        let jqfxhBgkkView = UIView()
        jqfxhBgkkView.backgroundColor = UIColor.init(red: 0, green: 0, blue: 0, alpha: 0.3)
        return jqfxhBgkkView
    }()
    
    
    public lazy var jqfxhTableppView : UITableView = {
        let jqfxhTableppView = UITableView(frame: .zero, style: .grouped)
        jqfxhTableppView.register(JQFXHProtocoqqlItemCell.self, forCellReuseIdentifier: JQFXHProtocoqqlItemCellId)
        jqfxhTableppView.backgroundColor = UIColor.white
        jqfxhTableppView.estimatedRowHeight = 0.01;
        jqfxhTableppView.estimatedSectionHeaderHeight = 10;
        jqfxhTableppView.estimatedSectionFooterHeight = 10;
        jqfxhTableppView.tableHeaderView = UIView(frame: CGRect(x: 0, y: 0, width: jqfxhScreenWidth, height: 0.01))
        jqfxhTableppView.tableFooterView = UIView(frame: CGRect(x: 0, y: 0, width: jqfxhScreenWidth, height: 0.01))
        jqfxhTableppView.separatorStyle = .none
        jqfxhTableppView.showsVerticalScrollIndicator = false
        jqfxhTableppView.showsHorizontalScrollIndicator = false
        if #available(iOS 11.0, *) {
            jqfxhTableppView.contentInsetAdjustmentBehavior = .never
        } else {
        }
        
        if #available(iOS 15.0, *) {
            jqfxhTableppView.sectionHeaderTopPadding = 0.01;
        } else {
        }
        jqfxhTableppView.layer.cornerRadius = 8
        jqfxhTableppView.layer.masksToBounds = true
        jqfxhTableppView.isScrollEnabled = false
        jqfxhTableppView.delegate = self
        jqfxhTableppView.dataSource = self
        return jqfxhTableppView
    }()
    
    
    lazy var jqfxhAgreenmmButton: UIButton = {
        let jqfxhAgreenmmButton = UIButton(type: .custom)
        jqfxhAgreenmmButton.setTitle("同意", for: .normal)
        jqfxhAgreenmmButton.backgroundColor = UIColor.jqfxhThemexxRed
        jqfxhAgreenmmButton.setTitleColor(.white, for: .normal)
        jqfxhAgreenmmButton.layer.cornerRadius = 8
        jqfxhAgreenmmButton.addTarget(self, action: #selector(jqfxhNextBtnClick), for: .touchUpInside)
        return jqfxhAgreenmmButton
    }()
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}


extension JQFXHProtosggView : UITableViewDelegate {
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 44
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let item = self.jqfxhProtocoliiList[indexPath.row]
        let web = JQFXHWebViewController(jqfxhUrlccString: item.protocolUrl)
        self.jqfxhVc?.present(web, animated: true)

    }
    
}

extension JQFXHProtosggView : UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        self.jqfxhProtocoliiList.count
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: JQFXHProtocoqqlItemCellId, for: indexPath) as! JQFXHProtocoqqlItemCell
        cell.jqfxhUpdaterrProtocol(protocolModel: self.jqfxhProtocoliiList[indexPath.row])
        return cell
    }
}


public let JQFXHProtocoqqlItemCellId = "JQFXHProtocoqqlItemCell"

class JQFXHProtocoqqlItemCell : UITableViewCell {
    
    func jqfxhUpdaterrProtocol(protocolModel:JQFXHSrwwProtocolModel) -> Void {
        self.jqfxhTitlennLabel.text = protocolModel.protocolName

    }
    

    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.selectionStyle = .none

        
        self.contentView.addSubview(jqfxhTitlennLabel)
        jqfxhTitlennLabel.snp.makeConstraints { make in
            make.left.equalTo(self.contentView).offset(16)
            make.centerY.equalTo(self.contentView)

        }
                
    }
   
    
    
   
    lazy var jqfxhBgkkView : UIView = {
        let jqfxhBgkkView = UIView()
        jqfxhBgkkView.backgroundColor = UIColor.white
        return jqfxhBgkkView
    }()
   
    lazy var jqfxhTitlennLabel : UILabel = {
        let jqfxhTitlennLabel = UILabel()
        jqfxhTitlennLabel.font = UIFont.jqfxhRegular16
        jqfxhTitlennLabel.textColor = UIColor.black
        return jqfxhTitlennLabel
    }()

    
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

